package com.dhcc.core.modules.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.constant.state.BizLogType;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.Bean2MapUtil;
import com.dhcc.core.modules.system.entity.OperationLog;
import com.dhcc.core.modules.system.service.IOperationLogService;
import com.dhcc.core.modules.system.warpper.LogWarpper;

/**
 * 日志管理的控制器
 * 
 * @ClassName: OperationLogController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:29:24
 */
@Controller
@RequestMapping("${sys.backendPath}/operation_log")
public class OperationLogController extends BaseController {

    private static String PREFIX = "/backend/system/log/";

    @Autowired
    private IOperationLogService operationLogService;

    /**
     * 跳转到日志管理的首页
     */
    @RequestMapping
    public String index() {
        return PREFIX + "operation_log.html";
    }

    /**
     * 查询操作日志列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/list")
    @Permission("operation:log:list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String logName, @RequestParam(required = false) Integer logType,@RequestParam(required = false) String patientId,@RequestParam(required = false) String patientName) {
        Page<OperationLog> page = new PageFactory<OperationLog>().defaultPage();
        List<Map<String, Object>> result = operationLogService.getOperationLogs(page, beginTime, endTime, logName,
                BizLogType.valueOf(logType),patientId,patientName);
        page.setRecords((List<OperationLog>) new LogWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 查询操作日志详情
     */
    @RequestMapping("/detail/{id}")
    @Permission("operation:log:detail")
    @ResponseBody
    public Object detail(@PathVariable Long id) {
        OperationLog operationLog = operationLogService.selectById(id);
        Map<String, Object> stringObjectMap = Bean2MapUtil.toMap(operationLog);
        return super.warpObject(new LogWarpper(stringObjectMap));
    }

    /**
     * 清空日志
     */
    @BussinessLog(value = "清空业务日志")
    @RequestMapping("/delete")
    @Permission(roles = Const.ADMIN_NAME)
    @ResponseBody
    public Object delLog() {
        SqlRunner.db().delete("delete from sys_operation_log");
        return R.ok();
    }
}
